/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.controller;

import com.tarena.lbs.attach.pojo.vo.FileVO;
import com.tarena.lbs.attach.service.AttachService;
import com.tarena.lbs.base.protocol.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

//@Api(value = "attach", tags = "附件中心管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/admin/attach")
@Tag(name = "附件服务", description = "附件中心管理")
public class AttachController {

    @Resource
    private AttachService attachService;
    /**
     * 文件上传接口
     * @param file 上传的文件（必填）
     * @param businessType 业务类型（可选，如1=商家Logo，2=门店图片）
     * @param businessId 关联业务ID（可选，如门店ID）
     */
    @PostMapping("upload")
    @Operation(summary = "上传附件")
    public Result<FileVO> uploadAttachInfo(
            @NotNull(message = "文件不能为空") @RequestPart("file") MultipartFile file,
//            @RequestParam(value = "moduleType", required = false) Integer moduleType, // 新增：文件用途类型
            @RequestParam(value = "businessType", required = false) Integer businessType,
            @RequestParam(value = "businessId", required = false) Integer businessId
    ) {
        try {
            System.out.println();
            System.out.println("----uploadAttachInfo----");
            System.out.println("文件上传业务处理,入参:file={},businessType={},businessId={}"+ file+businessType+businessId);
            FileVO fileVO = attachService.upload(file, businessType, businessId);
            System.out.println("fileVO:fileVO::fileVO:::"+fileVO);
            return new Result<>(fileVO); // 成功返回文件信息
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return Result.fail("-1", "上传失败：" + e.getMessage()); // 失败返回错误信息
        }
    }

    /**
     * 文件删除接口
     * @param id 附件ID（attach表主键）
     */
    @PostMapping("remove")
    @Operation(summary = "删除附件")
    public Result<Boolean> removeAttach(
            @NotNull(message = "附件ID不能为空") @RequestParam Integer id
    ) {
        try {
            boolean success = attachService.delete(id);
            return success ? new Result<>(true) : Result.fail("-1", "文件不存在或已删除");
        } catch (Exception e) {
            log.error("文件删除失败", e);
            return Result.fail("-1", "删除失败：" + e.getMessage());
        }
    }
}